import {Component, OnInit} from '@angular/core';
import {FileInputPluginService} from "../share/file-input-plugin.service";
import {url} from '../share/url';
import {HttpService} from "../share/http.service";
declare var $: any;


@Component({
  moduleId: module.id,
  selector: 'my-money_manage',
  templateUrl: 'money_manage.component.html'
})

export class money_manageComponent implements OnInit {
  public getBannerImageUrl: string = url.url+"/";
  public getBannerImageParams: any = "";
  public unloadImageArray: Array<object> = [];
  public caseBannerImageArray: Array<object> = [];
  public serviceBannerImageArray: Array<object> = [];
  public aboutUsBannerImageArray: Array<object> = [];
  public newsListBannerImageArray: Array<object> = [];
  public concatUsBannerImageArray: Array<object> = [];
  public indexBannerImageArray: Array<object> = [];
  public firstUnloadImageArray: any = [];

  constructor(private fileInputPlugin: FileInputPluginService,
              private http: HttpService) {
  }

  ngOnInit() {
    this.http.httpGet(this.getBannerImageUrl, this.getBannerImageParams)
      .subscribe((res) => {
        console.log(res);
        let value = res.results;
        this.firstUnloadImageArray = this.firstUnloadImageArray.concat(value);


<<<<<<< HEAD
      });
    this.loadPlugin(this.caseBannerImageArray, this.serviceBannerImageArray, this.aboutUsBannerImageArray, this.newsListBannerImageArray, this.concatUsBannerImageArray, this.indexBannerImageArray);
=======

      });
    this.loadPlugin(this.caseBannerImageArray,this.serviceBannerImageArray,this.aboutUsBannerImageArray,this.newsListBannerImageArray,this.concatUsBannerImageArray,this.indexBannerImageArray);


>>>>>>> 5f3ac8f2aa524649cf71e4329f79f1081d6b2514


    this.fileInputPluginUploaded("#input-gly-3", 'caseBanner');
    this.fileInputPluginUploaded("#input-gly-4", 'serviceBanner');
    this.fileInputPluginUploaded("#input-gly-5", 'aboutUsBanner');
    this.fileInputPluginUploaded("#input-gly-6", 'newsListBanner');
    this.fileInputPluginUploaded("#input-gly-7", 'concatUsBanner');
    this.fileInputPluginUploaded("#input-gly-8", 'indexBanner');

    this.fileInputPluginDelete("#input-gly-3");
    this.fileInputPluginDelete("#input-gly-4");
    this.fileInputPluginDelete("#input-gly-5");
    this.fileInputPluginDelete("#input-gly-6");
    this.fileInputPluginDelete("#input-gly-7");
    this.fileInputPluginDelete("#input-gly-8");
  }

  loadPlugin(array3, array4, array5, array6, array7, array8) {
    this.fileInputPlugin.showFileInputPlugin("#input-gly-3", 1, array3, []);
    this.fileInputPlugin.showFileInputPlugin("#input-gly-4", 1, array4, []);
    this.fileInputPlugin.showFileInputPlugin("#input-gly-5", 1, array5, []);
    this.fileInputPlugin.showFileInputPlugin("#input-gly-6", 1, array6, []);
    this.fileInputPlugin.showFileInputPlugin("#input-gly-7", 1, array7, []);
    this.fileInputPlugin.showFileInputPlugin("#input-gly-8", 10, array8, []);
  }

  fileInputPluginPredelete(ele) {
    $(ele).on("filepredelete", function (event, key, jqXHR, data) {
      console.log(this.firstUnloadImageArray);
      console.log(key);
      // let j:number;
      for (let i = 0, len = this.firstUnloadImageArray.length; i < len; i++) {
        let pName = this.firstUnloadImageArray[i].imgName;
        console.log(pName);
        if (pName === key) {
          // console.log(i);
          // j = i;
          this.firstUnloadImageArray.splice(i, 1);
          return;
        }
      }
      // this.firstUnloadImageArray.splice(j,1);
    }.bind(this))
  }

  fileInputPluginDelete(ele) {
    $(ele).on('filesuccessremove', function (event, data, id, index) {
      console.log(data);
      console.log(id);
      console.log(index);
      let deleteName = $('#' + data + ' img').attr('title');
      console.log(deleteName);
      let newArray = [];
      // this.firstUnloadImageArray = this.firstUnloadImageArray.filter((item)=>item.imgName === deleteName);
      for (let i = 0, len = this.firstUnloadImageArray.length; i < len; i++) {
        if (this.firstUnloadImageArray[i].imgName === deleteName) {
          this.firstUnloadImageArray.splice(i, 1);
        }
      }
      console.log(this.firstUnloadImageArray);
      // event.preventDefault();
      // console.log(index)
    }.bind(this))
  }


  fileInputPluginUploaded(ele, position) {
    $(ele).on("fileuploaded", function (a, data, c, d) {
      console.log("成功上传图片,获取数据");
      // console.log(data);
      // console.log(a);
      // console.log(c);
      // console.log(d);
      let obj: any = {};
      let image1Response = data.response;
      console.log(data);
      let pName: string = '';
      let files = data.files;
      console.log(files);
      for (let i = 0, len = files.length; i < len; i++) {
        if (files[i] !== 'undefined') {
          console.log(files[i]);
          pName = files[i].name;
          break;
        }
      }
      obj.address = image1Response.results.path;
      obj.imgName = pName;
      obj.position = position;
      this.firstUnloadImageArray.push(obj);
      // this.unloadImageArray.push(obj);
      console.log(this.firstUnloadImageArray);
      // this.image1DataFromModal.emit(image1Response)
    }.bind(this));
  }
}
